import { mapGetters, mapMutations, mapActions } from 'vuex'
export const lotterySelectionMixin = {}
export const ballAndSelectorMixin = {
  data() {
    return {
      lotteryCount: 0,
    }
  },
  methods: {
    getSelectedNumbers(newData) {
      this.numberInfo = []
      newData.forEach(item => {
        item.numbers.forEach(value => {
          if (value.selected) {
            let index = item.place ? item.place.position : 0
            if (!this.numberInfo[index]) {
              this.numberInfo[index] = {}
              this.numberInfo[index].position = item.place ? item.place.context : ''
              this.numberInfo[index].numbers = []
            } 
            let i = this.numberInfo[index].numbers.findIndex(item => {
              return item == value.number
            })
            if (i == -1) {
              this.numberInfo[index].numbers.push({
                number: value.number,
                price: 0
              })
            }
          } 
        })
      })
      console.log(this.numberInfo)
    },
    // 计选购数量/总价
    countLottery() {
      let quantity = 0
      let totalPrice = 0
      this.numberInfo.forEach(item => {
        quantity += item.numbers.length 
      }) 
      this.lotteryCount = quantity
      totalPrice = this.lotteryCount * this.getUnitPrice * this.getUnitRate
      this.setBuyingQuantity(this.lotteryCount)
      this.setTotalAmount(totalPrice)
    },
    ...mapMutations('lottery', {
      setBuyingQuantity: 'SET_BUYING_QUANTITY',
      setTotalAmount: 'SET_TOTAL_AMOUNT'
    }),
  },
  computed: {
    ...mapGetters('lottery', ['getScCgjQy', 'getUnitPrice', 'getUnitRate']),
  },
  watch: {
    // // 单价改变重新计算金额
    // 'getUnitPrice'(newPrice) {
    //   this.setSingleAmount(newPrice * this.lotteryCount * this.getUnitRate)
    // },
    // 'getUnitRate'(newVal) {
    //   this.setSingleAmount(newVal * this.lotteryCount * this.getUnitPrice)
    // }
  }
}

// 框+清空
export const boxWithCleanerMixin = {
  created() {
    this.setBuyingQuantity(this.lotteryCount)
  },
  methods: {
    handleUpdateData(item) {
      let id = item.id 
      this.cloendCurrentLotteryState.some((value, index) => {
        if (value.id == id) {
          value.numbers = item.numbers
        }
      })
      // 更新state中的彩票选中状态
      this.setCurrentLotteryState(this.cloendCurrentLotteryState)
      // 获取选择的彩票数据
      this.getSelectedNumbers(this.cloendCurrentLotteryState)
      // 保存选中彩票到state
      this.setSelectedNumbers(this.numberInfo)
      // 统计选择彩票数量
      this.countLottery()
    },
    getSelectedNumbers(newData) {
      this.numberInfo = []
      newData.forEach(item => {
        item.numbers.forEach(value => {
          if (value.selected) {
            let index = item.place ? item.place.position : 0
            if (!this.numberInfo[index]) {
              this.numberInfo[index] = {}
              this.numberInfo[index].position = item.place ? item.place.context : ''
              this.numberInfo[index].numbers = []
            } 
            this.numberInfo[index].numbers.push({
              number: value.number,
              price: value.unitPrice
            })
          } 
        })
      })
      console.log(this.numberInfo)
    },
    // 清除选中状态
    clearSelectionState() {
      this.cloendCurrentLotteryState.forEach(item => {
        item.numbers.forEach(number => {
          number.selected = false
        }) 
      })
      this.setCurrentLotteryState(this.cloendCurrentLotteryState)
      this.lotteryCount = 0
      this.setBuyingQuantity(this.lotteryCount)
    },
    // 计选购数量/总价
    countLottery() {
      let quantity = 0
      let totalPrice = 0
      this.numberInfo.forEach(item => {
        quantity += item.numbers.length 
        item.numbers.forEach(lottery => {
          totalPrice += lottery.price
        })
      }) 
      this.lotteryCount = quantity
      if (!totalPrice) {
        totalPrice = 0
      }
      this.setBuyingQuantity(this.lotteryCount)
      this.setTotalAmount(totalPrice)
    },
    ...mapMutations('lottery', {
      setCurrentLotteryState: 'SET_CURRENT_LOTTERY_STATE',
      setSelectedNumbers: 'SET_SELECTED_NUMBERS',
      setBuyingQuantity: 'SET_BUYING_QUANTITY',
      setTotalAmount: 'SET_TOTAL_AMOUNT'
    })
  },
  computed: {
    ...mapGetters('lottery', ['getCurrentLotteryState', 'getIsPhone']),
    cloendCurrentLotteryState() {
      return JSON.parse(JSON.stringify(this.getCurrentLotteryState))
    }
  }
}
// 球+快捷选号
export const ballWithSelectorMixin = {
  created() {
    this.setBuyingQuantity(this.lotteryCount)
    this.setRadioSelectorIndex(0)
  },
  methods: {
    handleUpdateData(item) {
      let id = item.id 
      this.clonedCurrentLotteryState.some((value, index) => {
        if (value.id == id) {
          value.numbers = item.numbers
        }
      })
      // 更新state中的彩票选中状态
      this.setCurrentLotteryState(this.clonedCurrentLotteryState)
      // 获取选择的彩票数据
      this.getSelectedNumbers(this.clonedCurrentLotteryState)
      //
       this.setSelectedNumbers(this.numberInfo)
       // 统计选择彩票数量
      this.countLottery()
    },
    getSelectedNumbers(newData) {
      this.numberInfo = []
      newData.forEach(item => {
        item.numbers.forEach(value => {
          if (value.selected) {
            let index = item.place ? item.place.position : 0
            if (!this.numberInfo[index]) {
              this.numberInfo[index] = {}
              this.numberInfo[index].position = item.place ? item.place.context : ''
              this.numberInfo[index].numbers = []
            } 
            this.numberInfo[index].numbers.push({
              number: value.number,
              price: value.unitPrice
            })
          } 
        })
      })
      console.log(this.numberInfo)
    },
    // 清除选中状态
    clearSelectionState() {
      this.clonedCurrentLotteryState.forEach(item => {
        item.numbers.forEach(number => {
          number.selected = false
        }) 
      })
      this.setCurrentLotteryState(this.clonedCurrentLotteryState)
      this.lotteryCount = 0
      this.setBuyingQuantity(this.lotteryCount)
    },
    // 计选购数量
    countLottery() {
      let quantity = 0
      this.numberInfo.forEach(item => {
        quantity += item.numbers.length 
      }) 
      this.lotteryCount = quantity
      this.setBuyingQuantity(this.lotteryCount)
    },
    ...mapMutations('lottery', {
      setCurrentLotteryState: 'SET_CURRENT_LOTTERY_STATE',
      setSelectedNumbers: 'SET_SELECTED_NUMBERS',
      setBuyingQuantity: 'SET_BUYING_QUANTITY',
      setRadioSelectorIndex: 'SET_RADIO_SELECTOR_INDEX'
    }),
    updateIndex(index) {
      this.activeSwitchIndex = index
      console.log(index)
    }
  },
  computed: {
    ...mapGetters('lottery', ['getCurrentLotteryState', 'getIsPhone', 'getRadioSelectorIndex']),
    clonedCurrentLotteryState() {
      return JSON.parse(JSON.stringify(this.getCurrentLotteryState))
    }
  }
}
// 添加彩票
export const addLotteryToCartMixin = {
  methods: {
    // 清空购彩信息
    clearBuyingInfo() {
      // 返点开关状态改变
      this.on = false
      this.unitPrice = 2
      let defaultInfo = {
        unitPrice: 2,
        unit: 1,
        refundRate: 0,
        totalAmount: 0
      }
      // 清空单价返点等信息
      this.clearInfo(defaultInfo)
    },
    // 添加到购物车
    addLotteryToCart() {
      this.numbers = []
      if(!this.getSelectedNumbers.length) {
        alert('请添加彩票')
      } else {
        for (let i = 0; i < this.getSelectedNumbers.length; i++) {
          if (this.getSelectedNumbers[i] == undefined) {
            continue;
          }
          this.numbers.push(this.getSelectedNumbers[i])
        }
        this.addToCart(this.getCompleteLotteryInfo())
        this.clearSelectionState()
        // this.clearBuyingInfo()
      }
    },
    // 完整购彩信息
    getCompleteLotteryInfo() {
      let lotteryInfo = {
        LotteryType: this.getLotteryType,
        mainPlayMode: (this.getCurrentMainTab.mainTabName + '/' + this.getCurrentTab.subTabName),
        subPlayMode: this.getCurrentTab.subTabName,
        numberStr: this.getNumberStr(),
        detailNumberInfo: this.getDetailNumberStr(),
        refundRate: this.getRefundRate,
        unitPrice: this.getUnitPrice,
        buyingQuantity: this.getBuyingQuantity,
        totalAmount: parseInt(this.getTotalAmount) || parseInt(this.getUnitPrice * this.getBuyingQuantity),
        bonous: this.getBonous
      }
      return lotteryInfo
    },
    // 彩票号码
    getNumberStr() {
      let numStr = ''
      this.numbers.forEach(number => {
        number.numbers.forEach(item => {
          numStr += item.number + ','
        }) 
      })
      return numStr.slice(0, numStr.length - 1)
    },
    //号码加位置
    getDetailNumberStr() {
      let detailInfo = ''
      this.numbers.forEach(number => {
        if (number.position) {
          detailInfo += number.position + ':'
          number.numbers.forEach(item => {
            detailInfo += item.number + ','
          }) 
        }
      })
      return detailInfo.slice(0, detailInfo.length - 1)
    },
    // 清空彩票选中状态
    clearSelectionState() {
      console.log('clear')
      this.clonedLotteryState.forEach(item => {
        item.numbers.forEach(number => {
          number.selected = false
          if (number.parameter) {
            number.parameter[0].number = number.parameter[0].parameter ? number.parameter[0].parameter : number.parameter[0].number
          } 
        })
      })
      this.setCurrentLotteryState(this.clonedLotteryState)
      this.setBuyingQuantity(0)
      this.clearBuyingInfo()
    },
    ...mapMutations('lottery', {
      setUnitPrice: 'SET_UNIT_PRICE',
      setUnitRate: 'SET_UNIT_RATE',
      setRefundRate: 'SET_REFUND_RATE',
      setBuyingQuantity: 'SET_BUYING_QUANTITY',
      setLotteryNumbers: 'SET_LOTTERY_NUMBERS',
      addToCart: 'ADD_TO_CART',
      setTotalAmount: 'SET_TOTAL_AMOUNT',
      setCurrentLotteryState: 'SET_CURRENT_LOTTERY_STATE',
    }),
    ...mapActions('lottery', {
      clearInfo: 'clearBuyingInfo'
    })
  },
  computed: {
    ...mapGetters('lottery', [
      'getUnitPrice', 
      'getUnitRate', 
      'getLotteryType',
      'getCurrentMainTab',
      'getCurrentTab',
      'getRefundRate',
      'getBuyingQuantity',
      'getLotteryAmount',
      'getSelectedNumbers',
      'getBonous',
      'getBallSelectionState',
      'getCurrentLotteryInfo',
      'getCurrentLotteryState',
      'getTotalAmount',
      'getShowMoneyInput'
    ]),
     clonedLotteryState() {
      return JSON.parse(JSON.stringify(this.getCurrentLotteryState))
    }
  }
}